<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="com.base.myproject.server.tools.*" %>
<%@ page import="com.base.myproject.client.jdbc.DataSet,java.util.Date" %>
<%@page import="java.text.SimpleDateFormat,java.util.Calendar,java.math.BigDecimal"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title></title>
<%
	String beforeIcode = request.getParameter("beforeIcode");
	String afterIcode = request.getParameter("afterIcode");
	DataSetTool DST = new DataSetTool();
	
	if (beforeIcode == null && afterIcode == null) {
		String sql = "select * from hr_salary_main_c order by icode desc";
		DataSet rs = DST.getDataSet(sql);
		beforeIcode = rs.getValue(1, "icode");
		afterIcode = rs.getValue(0, "icode");
	}
	
	String beforeUpcodeSql = "select * from hr_salary_main_c where icode = " + beforeIcode + "";
	String afterUpcodeSql = "select * from hr_salary_main_c where icode = " + afterIcode + "";
	String salarySql = "select bname, employno, employinnertitle,employlevel,work_experience, name, depName, sumSalary1, sumSalary2, (sumSalary2 - sumSalary1) as value from ( " + 
						"select sumSalary1,sumSalary2, beforeMonthSalary.bcode from ( " + 
						"	select sum(amount) as sumSalary1, bcode from hr_salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + beforeIcode + "' group by bcode " + 
						") as beforeMonthSalary " + 
						"inner join (  " + 
						"	select sum(amount) as sumSalary2, bcode from hr_salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + afterIcode + "' group by bcode " + 
						") as afterMonthSalary on sumSalary1 != sumSalary2 and beforeMonthSalary.bcode = afterMonthSalary.bcode " + 
						" " + 
						"union all  " + 
						" " + 
						"select sum(amount) as sumSalary1,0 as sumSalary2, bcode from  " + 
						"hr_salary salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + beforeIcode + "' " + 
						"and not exists(select * from hr_salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + afterIcode + "' and bcode = salary.bcode) " + 
						"group by bcode " + 
						" " + 
						"union all " + 
						" " + 
						"select 0 as sumSalary1,sum(amount) as sumSalary2, bcode from  " + 
						"hr_salary salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + afterIcode + "' " + 
						"and not exists(select * from hr_salary, hr_salary_main_c where upcode = code and hr_salary_main_c.icode = '" + beforeIcode + "' and bcode = salary.bcode) " + 
						"group by bcode " + 
						") as salary " + 
						"inner join  " + 
						"( " + 
						"	select bcode.bcode, employno, bcode.bname,systemid, up_department, employinnertitle,employlevel,work_experience from bcode_job, bcode where bcode.bcode = bcode_job.bcode " + 
						") as bcode_job  " + 
						"on salary.bcode = bcode_job.bcode " + 
						"inner join ( " + 
						"	select name, (select name from systeminfo where id = left(info.id,4)) as depName, id from systeminfo info " + 
						") as dep_name on id = bcode_job.systemid " + 
						"order by depName,name";
	DataSet salaryRs = DST.getDataSet(salarySql);
	DataSet beforeUpcodeRs = DST.getDataSet(beforeUpcodeSql);
	DataSet afterUpcodeRs = DST.getDataSet(afterUpcodeSql);
	String department = "";
	String division = "";
	String beforeUpcode = beforeUpcodeRs.getValue(0 , "code");
	String afterUpcode = afterUpcodeRs.getValue(0 , "code");
	
	//人员数统计
	int departmentCount = 0;
	int divisionCount = 0;
	int allCount = 0;
	
	//金额统计
	double departmentSalary1Sum = 0;
	double departmentSalary2Sum = 0;
	double departmentContrastSum = 0;
	
	double divisionSalary1Sum = 0;
	double divisionSalary2Sum = 0;
	double divisionContrastSum = 0;
	
	double allSalary1Sum = 0;
	double allSalary2Sum = 0;
	double allContrastSum = 0;
	
	int tableNumber = 0;

%>

<script language="javascript"> 
function showDetail(employno) {
	var x = event.clientX - 800;
	var y = event.clientY + 5;
	var height = window.innerHeight;
	if (y > height - 200) {
		y = y - 210;
	}
	var href = "salaryContrastDetail.jsp?beforeIcode=<%= beforeIcode %>&afterIcode=<%= afterIcode %>&employno=" + employno;
	document.getElementById('detail').style.left = x + "px";
	document.getElementById('detail').style.top = y + "px";
	document.getElementById('detail').src = href;
	document.getElementById('detail').style.display = "block";
	
}

function hideDetail() {
	document.getElementById('detail').style.display = "none";
}

function hve_display(t_id){//显示隐藏程序
	var t_id = document.getElementById(t_id);//表格ID
	if (t_id.style.display == "none") {//如果为隐藏状态
	t_id.style.display="";//切换为显示状态
	}
	else{//否则
	t_id.style.display="none";//切换为隐藏状态
	}
	}
</script>

<LINK href="report.css" rel=stylesheet>
</head>
<body>

<div>
	<iframe height="200" width="800" id="detail" style="background-color: #FFFFFF;position: fixed;display: none" src="rptSalaryContrast.jsp"></iframe>
	<table id="tableExcel" style="border-collapse: collapse" width="100%"
		border="1" cellspacing="0" cellpadding="3" bordercolordark="#CCCCCC"
		bordercolorlight="#CCCCCC"
		style="line-height: 120%;border-color:#000000">
		<tr bgcolor="#e8e8e0" align="center">
					<td height="24" width="10%">部门</td>
					<td width="10%">分部</td>
					<td width="44%" colspan="5"></td>
					<td width="10%"><%= beforeUpcode %>工资汇总</td>
					<td width="10%"><%= afterUpcode %>工资汇总</td>
					<td width="8%">差额</td>
					<td width="8%"></td>
		</tr>
		
		<tr>
					<td height="24" colspan="11"></td>
		</tr>
		
		<% for (int i = 0; i < salaryRs.getRowCount(); i++) { 
			String newDepartment = salaryRs.getValue(i, "depName");
			String newDivision = salaryRs.getValue(i, "name");
			
			divisionSalary1Sum += Double.parseDouble(salaryRs.getValue(i, "sumSalary1"));
			divisionSalary2Sum += Double.parseDouble(salaryRs.getValue(i, "sumSalary2"));
			divisionContrastSum += Double.parseDouble(salaryRs.getValue(i, "value"));
			
			divisionCount++;
			departmentCount++;
			
			if (!department.equals(newDepartment)) {
				department = newDepartment;
			}
			
			if (!division.equals(newDivision)) {
				division = newDivision;
				tableNumber = i;
		%>
			<tr>
				<td colspan="11">
				<table width="100%" id="table<%= i %>"  style="border-collapse: collapse;line-height: 150%;border-color:#000000;display:none"
				border="1">
				<tr bgcolor="#e8e8e0" align="center">
					<td height="18" width="10%">部门</td>
					<td width="10%">分部</td>
					<td width="10%">人员编号</td>
					<td width="10%">人员姓名</td>
					<td width="8%">职位名称</td>
					<td width="8%">雇佣级别</td>
					<td width="8%">工作年限</td>
					<td width="10%"><%= beforeUpcode %>工资</td>
					<td width="10%"><%= afterUpcode %>工资</td>
					<td width="8%">差额</td>
					<td width="8%">工资明细</td>
				</tr>
	<%
		}
	%>
			<tr>
			<td height="18" width="10%"></td>
			<td width="10%"></td>
			<td width="10%"><%= salaryRs.getValue(i, "employno") %></td>
			<td width="10%"><%= salaryRs.getValue(i, "bname") %></td>
			<td width="8%"><%= salaryRs.getValue(i, "employinnertitle") %></td>
			<td width="8%"><%= salaryRs.getValue(i, "employlevel") %></td>
			<td width="8%"><%= salaryRs.getValue(i, "work_experience") %></td>
			<td width="10%"><%=RPTString2Double.string2String(salaryRs.getValue(i,
						"sumSalary1"))%></td>
			<td width="10%"><%=RPTString2Double.string2String(salaryRs.getValue(i,
						"sumSalary2"))%></td>
			<td width="8%"><%=RPTString2Double.string2String(salaryRs.getValue(i,
						"value"))%></td>
			<td width="8%"><a href="#" onmouseover="showDetail('<%= salaryRs.getValue(i, "employno") %>')" onmouseout="hideDetail()">明细</a></td>
			</tr>
			
			<%
				if ((i + 1) == salaryRs.getRowCount()
							|| !division.equals(salaryRs.getValue(i + 1, "name"))) {
			%>
				</table>
				</td>
			</tr>
			<tr bgcolor="#cccc77" onclick="hve_display('table<%= tableNumber %>')">
				<td width="10%" height="18"></td>
				<td width="10%"><%=division%>:<%=divisionCount%></td>
				<td width="44%" colspan="5"></td>
				<td width="10%"><%= RPTString2Double.double2String(divisionSalary1Sum) %></td>	
				<td width="10%"><%= RPTString2Double.double2String(divisionSalary2Sum) %></td>
				<td width="8%"><%= RPTString2Double.double2String(divisionContrastSum) %></td>
				<td width="8%"></td>
			</tr>
				
			<%
					divisionCount = 0;
					departmentSalary1Sum += divisionSalary1Sum;
					departmentSalary2Sum += divisionSalary2Sum;
					departmentContrastSum += divisionContrastSum;
					
					divisionSalary1Sum = 0;
					divisionSalary2Sum = 0;
					divisionContrastSum = 0;
				}
				if ((i + 1) == salaryRs.getRowCount()
						|| !department.equals(salaryRs.getValue(i + 1,
								"depName"))) {
			%>
			<tr bgcolor="#cccc77">
				<td height="24"><%=department%>:<%=departmentCount%></td>
				<td colspan="6"></td>
				<td><%= RPTString2Double.double2String(departmentSalary1Sum) %></td>
				<td><%= RPTString2Double.double2String(departmentSalary2Sum) %></td>
				<td><%= RPTString2Double.double2String(departmentContrastSum) %></td>
				<td></td>
			</tr>
			
		<%
				allCount += departmentCount;
				allSalary1Sum += departmentSalary1Sum;
				allSalary2Sum += departmentSalary2Sum;
				allContrastSum += departmentContrastSum;
		
				departmentCount = 0;
				departmentSalary1Sum = 0;
				departmentSalary2Sum = 0;
				departmentContrastSum = 0;
			}
		}
	%>
	
	<tr bgcolor="#cccc66">
		<td height="24">总数：<%= allCount %></td>
		<td colspan="6"></td>
		<td><%= RPTString2Double.double2String(allSalary1Sum) %></td>
		<td><%= RPTString2Double.double2String(allSalary2Sum) %></td>
		<td><%= RPTString2Double.double2String(allContrastSum) %></td>
		<td></td>
	</tr>
	</table>
</div>
</body>
</html>